$(function () {
    $("#jqGrid").Grid({
        url: '../export/list',
        colModel: [
            {
                label: 'id',
                name: 'id',
                index: 'id',
                key: true,
                hidden: true,
                width: 20
            },
            {
                label: '',
                name: 'groupId',
                index: 'group_id',
                width: 40
            },
            {
                label: '',
                name: 'tableId',
                index: 'table_id',
                width: 40
            },
            {
                label: '',
                name: 'dataDt',
                index: 'data_dt',
                width: 30
            },
            {
                label: '',
                name: 'remark',
                index: 'remark',
                width: 200
            }]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        taskLog: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        }
        ,
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.taskLog = {};
        },
        generate: function (event) {
            id = getSelectedRows("#jqGrid");
            console.log(id)
            if (id == null) {
                return;
            }
            confirm('确定要立即执行选中的记录？', function () {
                Ajax.request({
                    url: "../export/run",
                    params: {
                        "tableId": id.toString()
                    },
                    contentType: "application/json",
                    type: 'GET',
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        }
        ,
        download: function (event) {
            id = getSelectedRows("#jqGrid");
            console.log(id)
            if (id == null) {
                return;
            }
            var a = document.createElement('a')
            a.href = "../export/download?tableId=" + "\"" + encodeURI(id.toString()) + "\""
            a.click();
            vm.reload();
        },
        update: function (event) {
            id = getSelectedRow("#jqGrid");


            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm
                .taskLog.id ==
            null ? "../tasklog/save" : "../tasklog/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.taskLog),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            id = getSelectedRows("#jqGrid");
            if (id == null) return;


            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../tasklog/delete",
                    params: JSON.stringify(id),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "tasklog/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.taskLog = r.taskLog;
                }
            })
            ;
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});